package cn.sumpu.app.coolsk.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.sumpu.app.coolsk.biz.ScenicBiz;
import cn.sumpu.app.coolsk.constant.PageSize;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.dao.ScenicDao;
import cn.sumpu.app.coolsk.dao.TicketDao;
import cn.sumpu.app.coolsk.domain.Scenic;
import cn.sumpu.app.coolsk.domain.Ticket;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class ScenicBizImpl implements ScenicBiz{
	@Autowired
	private ScenicDao scenicDao;
	@Autowired
	private IdentifierBizImpl identifierBiz;
	@Autowired
	private TicketDao ticketDao;
	
	public void setScenicDao(ScenicDao scenicDao) {
		this.scenicDao = scenicDao;
	}
	public void setTicketDao(TicketDao ticketDao) {
		this.ticketDao = ticketDao;
	}

	@Override
	public Scenic saveScenic(Scenic scenic) throws AppBusinessException {
		if(scenic!=null){
			if(!Common.empty(scenic.getId())){
				scenicDao.updateScenic(scenic);
			}else{
				scenic.setId(identifierBiz.generateId(Table.SCENIC));
				scenicDao.insertScenic(scenic);
			}
		}
		return scenic;
	}

	@Override
	public Scenic getScenicById(String id) throws AppBusinessException {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", id);
		List<Scenic> list=scenicDao.getScenic(map);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Scenic delScenicById(String id) throws AppBusinessException {
		Scenic scenic=getScenicById(id);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", id);
		scenicDao.delScenic(map);
		return scenic;
	}

	@Override
	public PageModel getScenic(Integer page, Integer pagesize, String name) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		int size = pagesize;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		if(!Common.empty(name)){
			map.put("name", "%"+name+"%");
		}
		int count = scenicDao.getScenicCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(scenicDao.getScenic(map));
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}
	
	@Override
	public List<Ticket> getScenicTicket(String scId)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("scenic", scId);
		return ticketDao.getTicket(map);
	}
	@Override
	public PageModel getScenic(Integer page, Integer pagesize, String name,
			String city) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>(); 
		int size = pagesize;
		page = page==null ? 1:page;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		map.put("condition", "gmt_created");
		map.put("sort", "desc");
		if(!Common.empty(name)){
			map.put("name", "%"+name+"%");
		}
		if(!Common.empty(city)){
			map.put("city", city);
		}
		int count = scenicDao.getScenicCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(scenicDao.getScenic(map));
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}
	@Override
	public double getScenicPriceMin(String scenicId)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("scenic", scenicId);
		return scenicDao.getScenicPriceMin(map);
	}
	@Override
	public PageModel searchScenic(Integer page, String name,
			String city) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>(); 
		int size=PageSize.PAGE_SIZE_SEARCH;
		page = page==null ? 1:page;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		map.put("condition", "gmtCreated");
		map.put("sort", "desc");
		if(!Common.empty(name)){
			map.put("name", "%"+name+"%");
		}
		if(!Common.empty(city)){
			map.put("cityName", "%"+city+"%");
			map.put("provinceName", "%"+city+"%");
		}
		int count = scenicDao.searchScenicCount(map);
		List<Scenic> list=scenicDao.searchScenic(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(list);
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}
	@Override
	public List<Scenic> getScenic(Map<String, Object> map)
			throws AppBusinessException {
		return scenicDao.getScenic(map);
	}
	
	
}
